package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.dto.DeviceBaseDto;
import net.zigin.nhi.could.waste.service.DeviceBaseService;

import net.zigin.nhi.could.waste.vo.DeviceBaseVo;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * DeviceBase控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: DeviceBaseController
 * @date 2021-9-12 17:47:14
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/deviceBase")
public class DeviceBaseController extends BaseController {
    @Autowired
    private DeviceBaseService deviceBaseService;

    /**
     * 新增编辑设备
     *
     * @param deviceBaseDto 设备信息
     * @return 设备ID
     */
    @ApiOperation("新增编辑设备")
    @ApiImplicitParam(name = "deviceBaseDto", value = "设备信息", required = true, dataType = "DeviceBaseDto")
    @PostMapping("/save")
    public JsonForResult<String> sava(@RequestBody DeviceBaseDto deviceBaseDto) {
        String id = deviceBaseService.saveOrUpdate(deviceBaseDto);
        return JsonForResult.<String>builder().data(id).success(true).build();
    }

    /**
     * 设备列表
     *
     * @param deviceBaseDto 设备信息
     * @return 设备列表
     */
    @ApiOperation("设备列表")
    @ApiImplicitParam(name = "deviceBaseDto", value = "设备信息", required = true, dataType = "DeviceBaseDto")
    @PostMapping("/getList")
    public JsonForResult<List<DeviceBaseVo>> getList(@RequestBody DeviceBaseDto deviceBaseDto) {
        List<DeviceBaseVo> list = deviceBaseService.getList(deviceBaseDto);
        Integer count = deviceBaseService.getCount(deviceBaseDto);
        return JsonForResult.<List<DeviceBaseVo>>builder()
                .data(list).total(count)
                .success(true).build();
    }

    /**
     * 设备详情
     *
     * @param id 设备ID
     * @return 设备详情
     */
    @ApiOperation("设备详情")
    @ApiImplicitParam(name = "id", value = "设备id", required = true, dataType = "String")
    @GetMapping("/getById/{id}")
    public JsonForResult<DeviceBaseVo> getById(@PathVariable String id) {
        DeviceBaseVo deviceBaseVo = deviceBaseService.getInfoById(id);
        return JsonForResult.<DeviceBaseVo>builder().data(deviceBaseVo).success(true).build();
    }

    /**
     * 删除设备
     *
     * @param id 设备ID
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除设备")
    @ApiImplicitParam(name = "id", value = "设备id", required = true, dataType = "String")
    public JsonForResult remove(@PathVariable String id) {
        deviceBaseService.deleteByPrimaryKey(id);
        return JsonForResult.builder().success(true).build();
    }

    @ApiOperation("导出设备列表")
    @ApiImplicitParam(name = "deviceBaseDto", value = "设备信息", required = true, dataType = "DeviceBaseDto")
    @PostMapping("/exportList")
    public void exportList(@RequestBody DeviceBaseDto deviceBaseDto, HttpServletResponse response) {
        deviceBaseService.exportList(deviceBaseDto, response);
    }

    @LoginWhitePathAnnotation
    @ApiOperation("下载模板")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        deviceBaseService.downloadTemplate(response);
    }

    @ApiOperation("导入设备列表")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile")
    @PostMapping("importList")
    public JsonForResult testImport(@RequestParam("file") MultipartFile file) {
        deviceBaseService.importList(file);
        return JsonForResult.builder().success(true).build();
    }

}
